﻿/********************************************
Module Name:  Global Javascript Code
Created By:   Justin Jiang
Created Date: 2009-01-04
Description:
    1. Add a new row.
*********************************************/

function AddNewRow(newRowType, newProjectFlag, delConfirmMsg)
{
    var btn = event.srcElement;
    var tbls = document.getElementsByTagName("TABLE");
    var table = null;
    var i = 0;
    for(i = 0; i < tbls.length; i++)
    {
        if (tbls[i].id.indexOf("_RightGrid") >= 0)
        {
            table = tbls[i];
        }
    }

    // Left table
    var btnRow = btn.parentElement.parentElement;
    var rowIndex = btnRow.rowIndex - 1;
    if (btn.addRowNum == undefined)
    {
        btn.addRowNum = 0;
    }
    var rowAddedRowNumber = parseInt(btn.addRowNum);
    
    var row = btnRow.parentElement.rows[rowIndex];
    var newRow = row.cloneNode(true);
    ResetControls(newRow, rowAddedRowNumber);
    newRow.className = "tableRowNew";
    row.parentElement.insertBefore(newRow, row);
    
    // Right table
    var rightRow = table.rows[rowIndex];
    var rightNewRow = rightRow.cloneNode(true);
    ResetControls(rightNewRow, rowAddedRowNumber);
    rightNewRow.className = "tableRowNew";
    rightRow.parentElement.insertBefore(rightNewRow, rightRow);
    
    var btnDelete = document.createElement("INPUT");
    btnDelete.type = "button";
    btnDelete.value = "Delete";
    btnDelete.confirmMsg = delConfirmMsg;
    btnDelete.addButtonId = btn.id;
    btnDelete.onclick = OnDeleteProject;
    btnDelete.className = "InputButton";
    btnDelete.style.width = "70px";
    btnDelete.style.height = "20px";
    var lstTd = rightNewRow.childNodes[rightNewRow.childNodes.length - 1];
    lstTd.style.textAlign = "right";
    lstTd.childNodes[0].style.display = "none";
    lstTd.appendChild(btnDelete);
    
    var origionalItemNameColumn = GetItemNameColumn(row);
    
    // Add Hidden Fields
    // Add copy value columns
    var copyValueColumns = new Array("IsByProject", "CurrencyType", "EntryDefId", "EntryItemId");
    for(i = 0; i < copyValueColumns.length; i++)
    {
        
        var oValue = document.getElementById(origionalItemNameColumn.id.replace(/ItemName/g, copyValueColumns[i]));
        var nValue = oValue.cloneNode(true);
        nValue.id = origionalItemNameColumn.id.replace(/ItemName/g, copyValueColumns[i]) + "_" + rowAddedRowNumber;
        nValue.name = origionalItemNameColumn.name.replace(/ItemName/g, copyValueColumns[i]) + "_" + rowAddedRowNumber;
        oValue.parentElement.insertBefore(nValue, oValue);
    }
    
    // Add new value columns
    var oOldProject = document.getElementById(origionalItemNameColumn.id.replace(/ItemName/g, "OldProject"));
    var nOldProject = oOldProject.cloneNode(true);
    nOldProject.id = origionalItemNameColumn.id.replace(/ItemName/g, "OldProject") + "_" + rowAddedRowNumber;
    nOldProject.name = origionalItemNameColumn.name.replace(/ItemName/g, "OldProject") + "_" + rowAddedRowNumber;
    nOldProject.value = newProjectFlag;
    oOldProject.parentElement.insertBefore(nOldProject, oOldProject);
    
    var oRowType = document.getElementById(origionalItemNameColumn.id.replace(/ItemName/g, "RowType"));
    var nRowType = oRowType.cloneNode(true);
    nRowType.id = origionalItemNameColumn.id.replace(/ItemName/g, "RowType") + "_" + rowAddedRowNumber;
    nRowType.name = origionalItemNameColumn.name.replace(/ItemName/g, "RowType") + "_" + rowAddedRowNumber;
    nRowType.value = newRowType;
    oRowType.parentElement.insertBefore(nRowType, oRowType);
    
    // Reset AddedRowNumber
    btn.addRowNum = rowAddedRowNumber + 1;
    return false;
}

function ResetControls(row, rowAddedRowNumber)
{
    var inputs = row.getElementsByTagName("INPUT");
    var i = 0;
    for(i = 0; i < inputs.length; i++)
    {
        inputs[i].id += "_" + rowAddedRowNumber;
        inputs[i].name += "_" + rowAddedRowNumber;
        if (inputs[i].id.indexOf("_Project") > 0)
        {
            inputs[i].readOnly = false;
            inputs[i].setAttribute("clsC", "le");
            inputs[i].setAttribute("clsF", "li");
            inputs[i].setAttribute("clsH", "");
            inputs[i].className = "le";
        }
        if (inputs[i].id.indexOf("_P") > 0 ||
            inputs[i].id.indexOf("_FY") > 0)
        {
            inputs[i].value = "";
        }
    }
}

function GetItemNameColumn(row)
{
    var inputs = row.getElementsByTagName("INPUT");
    var i = 0;
    for(i = 0; i < inputs.length; i++)
    {
        if (inputs[i].id.indexOf("_ItemName") > 0)
        {
            return inputs[i];
        }
    }
}

function GetDeleteColumn(row)
{
    var inputs = row.getElementsByTagName("INPUT");
    var i = 0;
    for(i = 0; i < inputs.length; i++)
    {
        if (inputs[i].id.indexOf("_Delete") > 0)
        {
            return inputs[i];
        }
    }
}

function OnDeleteProject()
{
    var btnDelete = event.srcElement;
    if (confirm(btnDelete.confirmMsg))
    {
        var btnAdd = document.getElementById(btnDelete.addButtonId);
        var row = btnDelete.parentElement.parentElement;
        
        var origionalDeleteColumn = GetDeleteColumn(row);
        var origionalItemNameColumn = document.getElementById(origionalDeleteColumn.id.replace(/Delete/g, "ItemName"));
        var leftRow = origionalItemNameColumn.parentElement.parentElement;
        // Delete Hidden Fields
        // Delete copy value columns
        var copyValueColumns = new Array("IsByProject", "CurrencyType", "EntryDefId", "EntryItemId");
        for(i = 0; i < copyValueColumns.length; i++)
        {
            var oValue = document.getElementById(origionalItemNameColumn.id.replace(/ItemName/g, copyValueColumns[i]));
            oValue.parentElement.removeChild(oValue);
        }
        
        // Delete new value columns
        var oOldProject = document.getElementById(origionalItemNameColumn.id.replace(/ItemName/g, "OldProject"));
        oOldProject.parentElement.removeChild(oOldProject);
        
        var oRowType = document.getElementById(origionalItemNameColumn.id.replace(/ItemName/g, "RowType"));
        oRowType.parentElement.removeChild(oRowType);

        // Delete rows
        row.parentElement.deleteRow(row.rowIndex);
        leftRow.parentElement.deleteRow(leftRow.rowIndex);
    }
    return false;
}